package com.sunny.ynhz.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.sunny.ynhz.R;
import com.sunny.ynhz.ui.ImageActivity;

import java.util.List;

import uk.co.senab.photoview.PhotoView;


/**
 * Created by huhuan123 on 2018/3/30.
 */
public class MyViewPagerAdapter extends PagerAdapter {

    List<String> imgs;

    Context mContext;

    public MyViewPagerAdapter(Context context, List<String> imgs) {

        this.mContext = context;
        this.imgs = imgs;

    }

    @Override
    public int getCount() { // 获得size
        return imgs.size();
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == arg1;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        ((ViewPager) container).removeView((View) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        String imgUrl = imgs.get(position);
        View img1 = LayoutInflater.from(mContext).inflate(R.layout.imageview, null);
        PhotoView img = (PhotoView) img1.findViewById(R.id.iv_photo);
        img.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                ImageActivity mContext = (ImageActivity) MyViewPagerAdapter.this.mContext;
                mContext.showDigo(imgUrl);
                return true;
            }
        });
        Glide.with(mContext)
                .load(imgUrl)
                .fitCenter()
                .dontAnimate()
                .into(img);
        ((ViewPager) container).addView(img);

        return img;

    }
}
